<template>
  <div>
    <van-datetime-picker
      v-model="currentDate"
      type="date"
      title="选择年月日"
      :min-date="minDate"
      :max-date="maxDate"
      @confirm="onConfirm"
      @cancel="$emit('on-cancel')"
    />
  </div>
</template>

<script>
import { setUserProfile } from '@/api/user.js'
import { formatTime } from '@/filters'
export default {
  data() {
    return {
      currentDate: new Date(this.value),
      minDate: new Date('1900-01-01'),
      maxDate: new Date()
    }
  },

  props: {
    value: {
      type: String,
      retuired: true
    }
  },

  created() {

  },

  methods: {
    async onConfirm() {
      const birthday = formatTime(this.currentDate)
      this.$toast.loading({
        duration: 0, // 持续展示 toast
        forbidClick: true,
        message: '更改中...'
      })
      try {
        await setUserProfile({
          birthday
        })
        this.$toast('修改成功')
        this.$emit('on-cancel')
        this.$emit('input', birthday)
      } catch (err) {
        this.$toast('修改失败')
      }
    }
  }
}
</script>

<style scoped lang='less'>
</style>
